﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using Freshbox2._0.UI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class ReplenishController : BaseController<replenish>
    {
        //
        // GET: /Replenish/

        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<replenish> bll
        {
            get
            {
                return new ReplenishBll();
            }

        }

        public class replenishModel
        {
            public string waresCode { get; set; }
            public string waresName { get; set; }
            public string waresSpec { get; set; }
            public string waresUnit { get; set; }
            public int? waresquantity { get; set; }
            public long replenishID { get; set; }
            public decimal? waresPrice { get; set; }

            // waresCode   waresName    waresSpec  waresSpec waresquantity waresPrice  
        }

        WaresBll waresBll = new WaresBll();

        StoreHouseBll storeHouseBll = new StoreHouseBll();  

        // 获取商品不足的 列表
        public ActionResult GetLess(int page, int rows)
        {
           
            var replenishList = new List<replenishModel>();
            //先查出  区域 库存不足 的 商品信息    暂时先让他库存<10报警   之后再改    .Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();   rows   page
            var houseList = storeHouseBll.Search(x => x.validstatus == 1 && x.waresquantity < x.validstatus).Select(x => new { x.waresquantity, x.waresCode, x.areaCode, }).ToList();
            //再根据商品ID 查询replenish 表里面 state=0 待补购的 if  count=0  就add 一条记录
            foreach (var item in houseList)
            {
                //==0代表待补购的  如果没这个商品待补购 那么就插一条数据
                var repleList = bll.Search(x => x.waresCode == item.waresCode&&x.state==0).Select(x => new { x.waresCode, x.state }).ToList();
                if (repleList.Count==0)
                {
                    replenish remodel = new replenish();
                    //remodel.replenishID = remodel.replenishID;
                    remodel.state = 0;
                    remodel.waresCode = item.waresCode;
                    if (bll.Add(remodel) > 0)
                    {
                        
                    }
                }

            }
            //下面的步骤是为了  查询 列表数据
            //查询不足的商品
            var relist = bll.Search(x => x.state == 0).Select(x => new { x.replenishID, x.state, x.waresCode }).ToList();
            foreach (var item1 in relist)
            {
                //查询商品详细信息
                var wareList = waresBll.Search(x => x.waresCode == item1.waresCode).Select(x => new { x.waresName, x.waresCode, x.waresPrice, x.waresSpec, x.waresUnit }).ToList();
                int? num = storeHouseBll.Search(x => x.waresCode == item1.waresCode).Select(x => new { x.waresquantity }).FirstOrDefault().waresquantity;//小鸭说宿舍又没电了
                replenishModel model = new replenishModel();
                model.waresCode = item1.waresCode;
                model.replenishID =item1.replenishID;
                model.waresName = wareList.FirstOrDefault().waresName;
                model.waresPrice = wareList.FirstOrDefault().waresPrice;
                model.waresSpec = wareList.FirstOrDefault().waresSpec;
                model.waresUnit = wareList.FirstOrDefault().waresUnit;
                model.waresquantity = num;
                replenishList.Add(model); 
            }// 
            var orderList = replenishList.ToPagedList(page, rows).ToList();
            return Json(new { total = replenishList.Count, rows = orderList });
        }

        //提交补够数量 ReplenishNums
        public ActionResult SaveReplenishNum(ReplenishNums model)
        {
            string msg = "提交成功";
            //增加的记录  
            if (model.Updated != null)
            {
                foreach (var item in model.Updated)
                {
                    replenish re = new replenish();
                    re.replenishID =Convert.ToInt64( item.replenishID);
                    re.handleArea = 11001;
                    re.handleTime = DateTime.Now;
                    re.handleUserID =(int?)CurrentUer.adminId; 
                    re.replenishNum=item.replenishNum;
                    re.waresCode=item.waresCode;
                    re.state=1;
                    if (bll.Modify(re) > 0)
                    {

                    } 
                }
            }
            else
            {
                msg = "提交失败";
            }
            return Json(new { msg = msg });

        }




    }
}
